package com.smile.frame.common.page;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

/**
 * mybatisPlus 获取分页对象工厂
 *
 * @author LGC
 * @date 2021/7/21 14:01
 */
@Slf4j
public class PageFactory {

    /**
     * 根据自定义page查询对象获取分页对象
     *
     * @param pageQuery 自定义page查询对象
     * @return
     */
    public static <T> Page<T> getPageByPageQueryDto(PageQuery pageQuery) {
        return new Page<>(pageQuery.getPage_num(), pageQuery.getPage_size(), pageQuery.isSearch_total());
    }

    /**
     * 根据request请求获取分页对象
     *
     * @param request request请求
     * @return mybatisPlus Page对象
     */
    public static <T> Page<T> getPageByRequest(HttpServletRequest request) {
        //第几页
        long pageNum = 1;
        //每页条数
        long pageSize = 10;
        //是否查询总数 默认不查询总数
        boolean searchTotal = false;
        try {
            pageNum = Long.parseLong(request.getParameter("page_num"));
            pageSize = Long.parseLong(request.getParameter("page_size"));
            if (request.getParameter("search_total") != null) {
                searchTotal = Boolean.parseBoolean(request.getParameter("search_total"));
            }
        } catch (Exception e) {
            log.error("request page info error");
        }
        return new Page<>(pageNum, pageSize, searchTotal);
    }

}